﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Quiz.Model;
using Quiz.Player.Models;
using Quiz.Model.Repository;

namespace Quiz.Player.Controllers
{
    public class AnswerController : Controller
    {
        QuestionRepository _qRepository;
        public AnswerController(QuestionRepository queryRepository)
        {
            try
            {
                _qRepository = queryRepository;
            }
            catch
            {
            }
        }
        public AnswerController()
        {
            try
            {
                _qRepository = new QuestionRepository(AzureConfig.AzureAccount.CloudAccount.CreateCloudTableClient()); 
            }
            catch { 
            }
        }
        public Question FindQuestion()
        {
            string qId = Session["QuestionID"] as string;
            Question question = _qRepository.Find(x => x.RowKey == qId);
            return question;
        }
        public AnswerSet Find(string id)
        {
            Question question = FindQuestion();
            var anserList = question.GetPossibleAnswers();
            AnswerSet answerset = anserList.Where(x => x.AnswerSetID == id).FirstOrDefault();
            return answerset;
        }
        //
        // GET: /Answer/

        public ActionResult Index(string ID = null)
        {
            Question question = FindQuestion();
            return View(question.GetPossibleAnswers());
        }

        //
        // GET: /Answer/Details/5

        public ActionResult Details(string id = null)
        {
            AnswerSet answerset = Find(id);
            if (answerset == null)
            {
                return HttpNotFound();
            }
            return View(answerset);
        }

        //
        // GET: /Answer/Create

        public ActionResult Create()
        {
            AnswerSet answerset = new AnswerSet();
            answerset.QuestionID = Session["QuestionID"] as string;
            return View(answerset);
        }

        //
        // POST: /Answer/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(AnswerSet answerset)
        {
            if (ModelState.IsValid)
            {
                Question question = FindQuestion();
                var ansList = (List<AnswerSet>)question.GetPossibleAnswers();
                ansList.Add(answerset);
                question.AnswerDom = question.SetToAnswerDom(ansList);
                _qRepository.Update(question);
                return RedirectToAction("Index");
            }

            return View(answerset);
        }

        //
        // GET: /Answer/Edit/5

        public ActionResult Edit(string id = null)
        {
            AnswerSet answerset = Find(id);
            if (answerset == null)
            {
                return HttpNotFound();
            }
            return View(answerset);
        }

        //
        // POST: /Answer/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(AnswerSet answerset)
        {
            if (ModelState.IsValid)
            {
                Question question = FindQuestion();
                var ansList = (List<AnswerSet>)question.GetPossibleAnswers();
                var ans = ansList.Where(x => x.AnswerSetID == answerset.AnswerSetID).FirstOrDefault();
                
                ans.AnserType = answerset.AnserType;
                ans.Text =  answerset.Text;
                ans.Position = answerset.Position;
                question.AnswerDom = question.SetToAnswerDom(ansList);

                _qRepository.Update(question);
                return RedirectToAction("Index");
            }
            return View(answerset);
        }

        //
        // GET: /Answer/Delete/5

        public ActionResult Delete(string id = null)
        {
            AnswerSet answerset = Find(id);
            if (answerset == null)
            {
                return HttpNotFound();
            }
            return View(answerset);
        }

        //
        // POST: /Answer/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            AnswerSet answerset = Find(id);
            Question question = FindQuestion();
            var ansList = (List<AnswerSet>)question.GetPossibleAnswers();
            ansList.Remove(answerset);
            question.AnswerDom = question.SetToAnswerDom(ansList);
            _qRepository.Update(question);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _qRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}